<?php

namespace frontend\modules\Dao;

use common\models\TallyAccount;
use common\models\TallyRecord;

class TallyRecordDao extends BaseDao
{

    public static function get_builder($condition = []){
        $query = TallyRecord::find();


        if(self::checkIsHasField($condition,'begin_date')){

            $query->andFilterWhere(['>=','tally_time',$condition['begin_date']]);
        }

        if(self::checkIsHasField($condition,'end_date')){
            $query->andFilterWhere(['<=','tally_time',$condition['end_date']]);
        }
        if(self::checkIsHasField($condition,'tally_id')){
            $query->andFilterWhere(['tally_id'=>$condition['tally_id']]);
        }

        if(self::checkIsHasField($condition,'caterory_id')){
            $query->andFilterWhere(['caterory_id'=>$condition['caterory_id']]);
        }

        if(self::checkIsHasField($condition,'member_id')){
            $query->andFilterWhere(['member_id'=>$condition['member_id']]);
        }
        if(self::checkIsHasField($condition,'project_id')){
            $query->andFilterWhere(['project_id'=>$condition['project_id']]);
        }
        if(self::checkIsHasField($condition,'type')){
            $query->andFilterWhere(['type'=>$condition['type']]);
        }

        if(self::checkIsHasField($condition,'state')){
            $query->andFilterWhere(['state'=>$condition['state']]);
        }


        //如果有账户
        if(self::checkIsHasField($condition,'tag_name')){
            //查询账户的所有id
            $account_ids = TallyAccount::find()->where(['tag'=>$condition['tag_name'],'tally_id'=>$condition['tally_id']])
                           ->select('id')
                            ->column();
            $query->andFilterWhere([
                'in','account_id',$account_ids
            ]);
        }


        return $query;
    }


}